home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / UPC12BS1.ZIP / UUCICO / ULIBFS.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  22KB  |  597 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       u l i b f s . c                                              */
  3. /*                                                                    */
  4. /*       UUPC/extended communications driver for MS-DOS FOSSIL        */
  5. /*       based systems.                                               */
  6. /*--------------------------------------------------------------------*/
  7.  
  8. /*--------------------------------------------------------------------*/
  9. /*    Changes Copyright (c) 1989-1993 by Kendra Electronic            */
  10. /*    Wonderworks.                                                    */
  11. /*                                                                    */
  12. /*    All rights reserved except those explicitly granted by the      */
  13. /*    UUPC/extended license agreement.                                */
  14. /*--------------------------------------------------------------------*/
  15.  
  16. /*--------------------------------------------------------------------*/
  17. /*                          RCS Information                           */
  18. /*--------------------------------------------------------------------*/
  19.  
  20. /*
  21.  *    $Id: ulibfs.c 1.3 1993/10/03 22:09:09 ahd Exp $
  22.  *
  23.  *    History:
  24.  *    $Log: ulibfs.c $
  25.  * Revision 1.3  1993/10/03  22:09:09  ahd
  26.  * Use unsigned long to display speed
  27.  *
  28.  * Revision 1.2  1993/05/30  15:25:50  ahd
  29.  * Multiple driver support
  30.  *
  31.  * Revision 1.1  1993/05/30  00:01:47  ahd
  32.  * Initial revision
  33.  *
  34.  */
  35.  
  36. /*--------------------------------------------------------------------*/
  37. /*                        System include files                        */
  38. /*--------------------------------------------------------------------*/
  39.  
  40. #include <stdio.h>
  41. #include <string.h>        // For memset()
  42. #include <stdlib.h>        // For max()
  43. #include <dos.h>           // For FOSSIL interrupt calls
  44. #include <time.h>          // For sleep.h support
  45.  
  46. /*--------------------------------------------------------------------*/
  47. /*                    UUPC/extended include files                     */
  48. /*--------------------------------------------------------------------*/
  49.  
  50. #include "lib.h"
  51. #include "ulibfs.h"
  52. #include "commlib.h"
  53. #include "fossil.h"
  54. #include "catcher.h"       // For terminate processing flags
  55. #include "ssleep.h"        // For ddelay, etc.
  56.  
  57. /*--------------------------------------------------------------------*/
  58. /*                        Internal prototypes                         */
  59. /*--------------------------------------------------------------------*/
  60.  
  61. static void showModem( const short );
  62.  
  63. static void getDriverInfo( FS_INFO *fossilData);
  64.  
  65. static short blockIO( char *buffer,
  66.                       const short len,
  67.                       const char function);
  68.  
  69. /*--------------------------------------------------------------------*/
  70. /*                          Global variables                          */
  71. /*--------------------------------------------------------------------*/
  72.  
  73. static BPS currentBPS;
  74. static boolean currentDirect;
  75. static boolean carrierDetect;
  76. static boolean hangupNeeded;
  77.  
  78. currentfile();
  79.  
  80. /*--------------------------------------------------------------------*/
  81. /*    f o p e n L i n e                                               */
  82. /*                                                                    */
  83. /*    Open a fossil controlled port                                   */
  84. /*--------------------------------------------------------------------*/
  85.  
  86. int fopenline(char *name, BPS baud, const boolean direct)
  87. {
  88.    short result;
  89.    FS_INFO fossilData;         // Information returned by FOSSIL
  90.  
  91. /*--------------------------------------------------------------------*/
  92. /*                       Determine the port number                    */
  93. /*--------------------------------------------------------------------*/
  94.  
  95.    if (sscanf(name, "COM%d", &portNum) != 1)
  96.    {
  97.       printmsg(0,"Communications port must be format COMx, was %s",
  98.                 name);
  99.       panic();
  100.    }
  101.  
  102.    portNum--;                 // FOSSIL uses offset, not ordinal number
  103.  
  104. /*--------------------------------------------------------------------*/
  105. /*                   Attempt to initialize the driver                 */
  106. /*--------------------------------------------------------------------*/
  107.  
  108.    result = FSOpen( );        // Try to open the port
  109.    if ( result != FS_COOKIE )
  110.    {
  111.       printmsg(0,"fopenLine: Open failed, result %d",result );
  112.       return TRUE;            // Report failure
  113.    }
  114.  
  115. /*--------------------------------------------------------------------*/
  116. /*          Now initialize the rest of the port information           */
  117. /*--------------------------------------------------------------------*/
  118.  
  119.    ssleep(2);                 // Wait two seconds as required by V.24
  120.  
  121.    currentDirect = direct;    // Save for flow control processing
  122.    carrierDetect = FALSE;     // No modem connected yet
  123.  
  124.    flowcontrol( FALSE );      // Set no (or hardware) flow control
  125.    fSIOSpeed( baud );         // Set the port speed
  126.    traceStart( name );        // Enable line tracing
  127.  
  128.    getDriverInfo( &fossilData );
  129.  
  130.    printmsg(4,"fopenline: Driver: %Fs (revision %d)",
  131.                     fossilData.id,
  132.               (int) fossilData.revision );
  133.    printmsg(4,"fopenline: FOSSIL version %d, "
  134.               "input buffer %d, output buffer %d",
  135.               (int) fossilData.version,
  136.               (int) fossilData.inputSize,
  137.               (int) fossilData.outputSize );
  138.  
  139.    portActive = TRUE;         // Flag port is open
  140.    return FALSE;              // Report success to caller
  141.  
  142. } /* fopenLine */
  143.  
  144. /*--------------------------------------------------------------------*/
  145. /*       f s r e a d                                                  */
  146. /*                                                                    */
  147. /*       Read a specified number of bytes from the serial port        */
  148. /*--------------------------------------------------------------------*/
  149.  
  150. unsigned int fsread(char *buffer,
  151.                           unsigned int wanted,
  152.                           unsigned int timeout)
  153. {
  154.    FS_INFO fossilData;         // Information returned by FOSSIL
  155.  
  156.    time_t quit  = time( NULL ) + timeout;
  157.  
  158.    showModem( FSStatus());    // Report modem status if changed
  159.  
  160. /*--------------------------------------------------------------------*/
  161. /*                 Main loop to read data from FOSSIL                 */
  162. /*--------------------------------------------------------------------*/
  163.  
  164.    for ( ;; )
  165.    {
  166.       unsigned int pending;
  167.  
  168.       getDriverInfo( &fossilData );
  169.       pending = fossilData.inputSize - fossilData.inputFree;
  170.  
  171.       if ( pending >= wanted )
  172.       {
  173.          unsigned int moved = blockIO( buffer, wanted, FS_READBLOK );
  174.                                              // Get the data
  175.          traceData( buffer, moved, FALSE );  // Trace the data
  176.  
  177.          if ( moved < wanted)                // Promised data delivered?
  178.          {                                   // NO --> Panic (literally)
  179.             printmsg(0,"fsread: Read failed at %d of %d bytes"
  180.                        "(%d bytes available)",
  181.                         moved,
  182.                         wanted,
  183.                         pending );
  184.             panic();
  185.          }
  186.  
  187.          return pending;                            // Return success
  188.  
  189.       } /* if ( pending >= wanted ) */
  190.  
  191. /*--------------------------------------------------------------------*/
  192. /*       We don't have the data we need (yet), see if we can wait     */
  193. /*       for it.                                                      */
  194. /*--------------------------------------------------------------------*/
  195.  
  196.       if (quit <= time(NULL))             // Any time left to wait?
  197.       {
  198.          printmsg(20, "fsread: pending=%d, wanted=%d",
  199.                         pending, wanted);
  200.          return pending;                  // No --> return quietly
  201.       }
  202.  
  203.       if ( terminate_processing )         // User hit Ctrl-Break?
  204.